<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>cssspriteanimation.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_fx_cssspriteanimation.js.html">cssspriteanimation.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview An animation class that animates CSS sprites by changing the
<a name="line17"></a> * CSS background-position.
<a name="line18"></a> *
<a name="line19"></a> * @author arv@google.com (Erik Arvidsson)
<a name="line20"></a> * @see ../demos/cssspriteanimation.html
<a name="line21"></a> */
<a name="line22"></a>
<a name="line23"></a>goog.provide(&#39;goog.fx.CssSpriteAnimation&#39;);
<a name="line24"></a>
<a name="line25"></a>goog.require(&#39;goog.fx.Animation&#39;);
<a name="line26"></a>
<a name="line27"></a>
<a name="line28"></a>
<a name="line29"></a>/**
<a name="line30"></a> * This animation class is used to animate a CSS sprite (moving a background
<a name="line31"></a> * image).  This moves through a series of images in a single image sprite and
<a name="line32"></a> * loops the animation when done.  You should set up the
<a name="line33"></a> * {@code background-image} and size in a CSS rule for the relevant element.
<a name="line34"></a> *
<a name="line35"></a> * @param {Element} element The HTML element to animate the background for.
<a name="line36"></a> * @param {goog.math.Size} size The size of one image in the image sprite.
<a name="line37"></a> * @param {goog.math.Box} box The box describing the layout of the sprites to
<a name="line38"></a> *     use in the large image.  The sprites can be position horizontally or
<a name="line39"></a> *     vertically and using a box here allows the implementation to know which
<a name="line40"></a> *     way to go.
<a name="line41"></a> * @param {number} time The duration in milliseconds for one iteration of the
<a name="line42"></a> *     animation.  For example, if the sprite contains 4 images and the duration
<a name="line43"></a> *     is set to 400ms then each sprite will be displayed for 100ms.
<a name="line44"></a> * @param {function(number) : number=} opt_acc Acceleration function,
<a name="line45"></a> *    returns 0-1 for inputs 0-1.  This can be used to make certain frames be
<a name="line46"></a> *    shown for a longer period of time.
<a name="line47"></a> *
<a name="line48"></a> * @constructor
<a name="line49"></a> * @extends {goog.fx.Animation}
<a name="line50"></a> * @final
<a name="line51"></a> */
<a name="line52"></a>goog.fx.CssSpriteAnimation = function(element, size, box, time, opt_acc) {
<a name="line53"></a>  var start = [box.left, box.top];
<a name="line54"></a>  // We never draw for the end so we do not need to subtract for the size
<a name="line55"></a>  var end = [box.right, box.bottom];
<a name="line56"></a>  goog.fx.CssSpriteAnimation.base(
<a name="line57"></a>      this, &#39;constructor&#39;, start, end, time, opt_acc);
<a name="line58"></a>
<a name="line59"></a>  /**
<a name="line60"></a>   * HTML element that will be used in the animation.
<a name="line61"></a>   * @type {Element}
<a name="line62"></a>   * @private
<a name="line63"></a>   */
<a name="line64"></a>  this.element_ = element;
<a name="line65"></a>
<a name="line66"></a>  /**
<a name="line67"></a>   * The size of an individual sprite in the image sprite.
<a name="line68"></a>   * @type {goog.math.Size}
<a name="line69"></a>   * @private
<a name="line70"></a>   */
<a name="line71"></a>  this.size_ = size;
<a name="line72"></a>};
<a name="line73"></a>goog.inherits(goog.fx.CssSpriteAnimation, goog.fx.Animation);
<a name="line74"></a>
<a name="line75"></a>
<a name="line76"></a>/** @override */
<a name="line77"></a>goog.fx.CssSpriteAnimation.prototype.onAnimate = function() {
<a name="line78"></a>  // Round to nearest sprite.
<a name="line79"></a>  var x = -Math.floor(this.coords[0] / this.size_.width) * this.size_.width;
<a name="line80"></a>  var y = -Math.floor(this.coords[1] / this.size_.height) * this.size_.height;
<a name="line81"></a>  this.element_.style.backgroundPosition = x + &#39;px &#39; + y + &#39;px&#39;;
<a name="line82"></a>
<a name="line83"></a>  goog.fx.CssSpriteAnimation.base(this, &#39;onAnimate&#39;);
<a name="line84"></a>};
<a name="line85"></a>
<a name="line86"></a>
<a name="line87"></a>/** @override */
<a name="line88"></a>goog.fx.CssSpriteAnimation.prototype.onFinish = function() {
<a name="line89"></a>  this.play(true);
<a name="line90"></a>  goog.fx.CssSpriteAnimation.base(this, &#39;onFinish&#39;);
<a name="line91"></a>};
<a name="line92"></a>
<a name="line93"></a>
<a name="line94"></a>/**
<a name="line95"></a> * Clears the background position style set directly on the element
<a name="line96"></a> * by the animation. Allows to apply CSS styling for background position on the
<a name="line97"></a> * same element when the sprite animation is not runniing.
<a name="line98"></a> */
<a name="line99"></a>goog.fx.CssSpriteAnimation.prototype.clearSpritePosition = function() {
<a name="line100"></a>  var style = this.element_.style;
<a name="line101"></a>  style.backgroundPosition = &#39;&#39;;
<a name="line102"></a>
<a name="line103"></a>  if (typeof style.backgroundPositionX != &#39;undefined&#39;) {
<a name="line104"></a>    // IE needs to clear x and y to actually clear the position
<a name="line105"></a>    style.backgroundPositionX = &#39;&#39;;
<a name="line106"></a>    style.backgroundPositionY = &#39;&#39;;
<a name="line107"></a>  }
<a name="line108"></a>};
<a name="line109"></a>
<a name="line110"></a>
<a name="line111"></a>/** @override */
<a name="line112"></a>goog.fx.CssSpriteAnimation.prototype.disposeInternal = function() {
<a name="line113"></a>  goog.fx.CssSpriteAnimation.superClass_.disposeInternal.call(this);
<a name="line114"></a>  this.element_ = null;
<a name="line115"></a>};
</pre>


</body>
</html>
